package com.bocloud.cmp.driver.providers.os;

import java.util.List;

import org.openstack4j.api.Builders;
import org.openstack4j.model.common.ActionResponse;
import org.openstack4j.model.network.Subnet;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONObject;
import com.bocloud.cmp.driver.providers.SubnetDriver;
import com.bocloud.cmp.model.SubnetModel;
import com.bocloud.common.model.BsmResult;
import com.google.common.collect.Lists;

public class OSSubnetDriver extends OSDriver implements SubnetDriver {

	private static Logger logger = LoggerFactory.getLogger(OSSubnetDriver.class);

	public OSSubnetDriver() {
		super();
	}

	public OSSubnetDriver(String endpoint, String username, String password, String tenant, String domain,
			String project, Version version) {
		super(endpoint, username, password, tenant, domain, project, version);
	}

	@Override
	public BsmResult list() {
		List<SubnetModel> models = Lists.newArrayList();
		List<? extends Subnet> subnets = Lists.newArrayList();
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		subnets = this.getClient().networking().subnet().list();
		if (null != subnets) {
			for (Subnet subnet : subnets) {
				models.add(convertSubnet(subnet));
			}
		}
		return new BsmResult(true, models, "success");
	}

	@Override
	public BsmResult create(SubnetModel subnet) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		Subnet result = this.getClient().networking().subnet()
				.create(Builders.subnet().name(subnet.getName()).networkId(subnet.getNetworkId())
						.gateway(subnet.getGateway()).addPool(subnet.getStart(), subnet.getEnd())
						.ipVersion(org.openstack4j.model.network.IPVersionType.V4).cidr(subnet.getCidr())
						.enableDHCP(false).gateway(null).noGateway().build());
		if (null == result) {
			return new BsmResult(false, "创建失败！");
		} else {
			BsmResult detail = detail(result.getId());
			if (detail.isSuccess()) {
				return new BsmResult(true, detail.getData(), "create success");
			} else {
				return new BsmResult(false, "创建失败！");
			}
		}
	}

	@Override
	public BsmResult remove(String subnetId) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		ActionResponse response = this.getClient().networking().subnet().delete(subnetId);
		if (response.isSuccess()) {
			return new BsmResult(true, "success");
		} else {
			logger.error("remove subnet [{}] error: {}", subnetId, response.getFault());
			return new BsmResult(false, response.getFault());
		}
	}

	@Override
	public BsmResult detail(String subnetId) {
		if (!this.accessable()) {
			logger.error(this.getError());
			return new BsmResult(false, this.getError());
		}
		Subnet subnet = this.getClient().networking().subnet().get(subnetId);
		if (null == subnet) {
			return new BsmResult(false, "no such subnet");
		} else {
			return new BsmResult(true, convertSubnet(subnet), "success");
		}
	}

	private SubnetModel convertSubnet(Subnet subnet) {
		SubnetModel model = new SubnetModel();
		model.setName(subnet.getName());
		model.setCidr(subnet.getCidr());
		model.setDhcpEnabled(subnet.isDHCPEnabled());
		model.setDnsNames(JSONObject.toJSONString(subnet.getDnsNames()));
		model.setSubnetId(subnet.getId());
		model.setIpVersion(subnet.getIpVersion().name());
		model.setNetworkId(subnet.getNetworkId());
		model.setGateway(subnet.getGateway());
		model.setHostRoutes(JSONObject.toJSONString(subnet.getHostRoutes().toString()));
		return model;
	}
}
